﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class TFSQFill : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillFormData();
            }
        }

        private void FillFormData()
        {
            long applicationID = Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]);
            TFSQService service = new TFSQService();
            SpecialServiceForm form = service.GetSpecialServiceForm(applicationID);
            SQRLbl.Text = form.User.ContactPerson;
            CustomerContactLbl.Text = form.ApplicationForm.User.ContactPerson;
            CustomerLbl.Text = form.ApplicationForm.User.FullName;
            CustomerPhoneLbl.Text = form.ApplicationForm.User.UserMobilePhone;
            CustomerEmailLbl.Text = form.ApplicationForm.User.UserEmail;

            double totalCost = 0;
            foreach (SpecialServiceProduct serviceProduct in form.SpecialServiceProduct)
            {
                if (!serviceProduct.ApplicationProductReference.IsLoaded)
                {
                    serviceProduct.ApplicationProductReference.Load();
                }

                if (!serviceProduct.ApplicationProduct.QuatationForm.IsLoaded)
                {
                    serviceProduct.ApplicationProduct.QuatationForm.Load();
                }

                foreach (QuatationForm quatationForm in serviceProduct.ApplicationProduct.QuatationForm)
                {
                    totalCost += quatationForm.Cost;
                }
            }
            costLbl.Text = totalCost.ToString() + "元";
            RMALbl.Text = form.ApplicationForm.RunningNumber;

            ApplicationProductService productService = new ApplicationProductService();
            applicationProductGridView.DataSource = productService.GetQuatationProducts(form.ApplicationForm.ApplicationID);
            applicationProductGridView.DataBind();
        }

        protected void applicationProductGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();                
            }
        }


        protected void okLinkButton_Click(object sender, EventArgs e)
        {
            long applicationID = Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]);
            TFSQService service = new TFSQService();
            SpecialServiceForm form = service.GetSpecialServiceForm(applicationID);
            form.SellerManager = JXSManagerTxtBox.Text;
            form.SellerName = JXSTxtBox.Text;
            form.SellerPhone = JXSPhoneTxtBox.Text;
            form.ApplyReason = ReasonTxtBox.Text;
            form.FillDate = DateTime.Now;
            if (form.TFSQContent != null && form.TFSQContent.Count > 0)
            {
                form.TFSQContent.First().Cost = Convert.ToDecimal(costLbl.Text.Replace("元", ""));
                form.TFSQContent.First().Department = departmentTxtBox.Text;
                form.TFSQContent.First().StartDate = Convert.ToDateTime(startDateTxtBox.Text);
                form.TFSQContent.First().EndDate = Convert.ToDateTime(endDateTxtBox.Text);
                form.TFSQContent.First().FWContent = fwnrTxtBox.Text;
            }
            else
            {
                TFSQContent content = new TFSQContent();
                content.Cost = Convert.ToDecimal(costLbl.Text.Replace("元", ""));
                content.Department = departmentTxtBox.Text;
                content.StartDate = Convert.ToDateTime(startDateTxtBox.Text);
                content.EndDate = Convert.ToDateTime(endDateTxtBox.Text);
                content.FWContent = fwnrTxtBox.Text;
                form.TFSQContent.Add(content);
            }
            form.ContentComment = commentTxtBox.Text;
            form.Status = "3";
            service.UpdateForm(form);

            // 发送邮件给第一位审批人
            EmailService.SendEmail(form.User1.UserEmail, WebConfigurationManager.AppSettings["TFSQSHNoticeSubject"],
                    string.Format(WebConfigurationManager.AppSettings["TFSQSHNoticeBody"], form.User1.ContactPerson, form.ApplicationCode));

            Response.Write(string.Format("<script language='JavaScript'> alert( '记录已成功保存！');location.href= 'TFSQFillList.aspx' </script>"));
        }
    }
}
